package com.csj.mvvmtest.ui

import android.annotation.SuppressLint
import android.graphics.Bitmap
import android.text.TextUtils
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.webkit.WebChromeClient
import android.webkit.WebSettings
import android.webkit.WebView
import android.webkit.WebViewClient
import android.widget.ProgressBar
import androidx.core.content.res.ResourcesCompat
import com.csj.mvvm.ui.base.BaseActivity
import com.csj.mvvm.ui.base.BaseViewModel
import com.csj.mvvmtest.R
import com.csj.mvvmtest.databinding.ActivityWebviewBinding
import com.csj.mvvm.utils.LogUtil


class WebViewActivity : BaseActivity<BaseViewModel, ActivityWebviewBinding>() {
    private var progressbar: ProgressBar? = null
    private var startUrl: String? = null

    override fun initClick() {
    }

    @SuppressLint("SetJavaScriptEnabled")
    override fun initData() {
        progressbar = ProgressBar(mContext, null, android.R.attr.progressBarStyleHorizontal)
        progressbar!!.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            10
        )
        progressbar!!.progressDrawable =
            ResourcesCompat.getDrawable(resources, R.drawable.progress_bar, null)
        v.wv.addView(progressbar)

        var url = intent.getStringExtra("url")
        val title = intent.getStringExtra("title")
        LogUtil.e("url$url")
        LogUtil.e("title$title")
        v.bar.tvTopBarTitle.text = title
//        v.wv.settings.defaultTextEncodingName = "UTF-8"
        v.wv.settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
//        v.wv.settings.layoutAlgorithm = WebSettings.LayoutAlgorithm.SINGLE_COLUMN
        v.wv.settings.domStorageEnabled = true
        v.wv.settings.javaScriptEnabled = true
        v.wv.webChromeClient = (object : WebChromeClient() {
            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                if (newProgress == 100) {
                    progressbar!!.visibility = View.GONE
                } else {
                    if (progressbar!!.visibility == View.GONE)
                        progressbar!!.visibility = View.VISIBLE
                    progressbar!!.progress = newProgress
                }
                super.onProgressChanged(view, newProgress)
            }
        })
        v.wv.webViewClient = (object : WebViewClient() {
            //返回值为true导致当前WebView中止加载URL，返回false导致WebView继续加载当前URL
            override fun shouldOverrideUrlLoading(view: WebView, url: String): Boolean {
                LogUtil.d("shouldOverrideUrlLoading=$url")
                return false
            }
        })
        if (url == null) {
            if (title == "服务协议") {
                url = "file:///android_asset/sla.html"
            } else if (title == "隐私政策") {
                url = "file:///android_asset/privacy.html"
            }
        }
        v.wv.loadUrl(url!!)
    }

    override fun initVM() {
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {
        if (keyCode == KeyEvent.KEYCODE_BACK && v.wv.canGoBack()) {
            // 返回上一页面
            v.wv.goBack()
            return true
        }
        return super.onKeyDown(keyCode, event)
    }

    override fun onClickFinish(view: View?) {
        if (v.wv.canGoBack()) {
            // 返回上一页面
            v.wv.goBack()
        } else {
            finish()
        }
    }

}